package com.bwie.shop.service.impl;

import com.bwie.common.domain.Card;
import com.bwie.common.domain.Coupon;
import com.bwie.common.domain.Shop;
import com.bwie.common.domain.request.CouponRequest;
import com.bwie.common.domain.request.ShopRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.shop.mapper.ShopMapper;
import com.bwie.shop.service.ShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Result<PageResult<Shop>> showShop(ShopRequest shopRequest) {

        PageHelper.startPage(shopRequest.getPageNum(),shopRequest.getPageSize());

        List<Shop> shopList = shopMapper.showShop(shopRequest);
        PageInfo<Shop> info = new PageInfo<>(shopList);

        return PageResult.toResult(info.getTotal(),shopList);
    }

    /**
     * 商品的购买
     * @param shopId
     * @return
     */
    @Override
    public Result findById(String shopId) {
        Shop byId = shopMapper.findById(shopId);
        return Result.success(byId);
    }

    /**
     * 优惠卷列表
     * @return
     */
    @Override
    public Result showCoupon() {
        List<Coupon> coupons = shopMapper.showCoupon();
        return Result.success(coupons);
    }

    /**
     * 添加优惠券
     * @param couponRequest
     * @return
     */
    @Override
    public Result addCoupon(CouponRequest couponRequest) {
        Integer integer = shopMapper.addCoupon(couponRequest);
        return Result.success(integer);
    }

    /**
     * 优惠券的删除
     * @param couponId
     * @return
     */
    @Override
    public Result deleteCoupon(String couponId) {
        Integer integer = shopMapper.deleteCoupon(couponId);
        return Result.success(integer);
    }


    /**
     * 我的优惠券
     * @return
     */
    @Override
    public Result showCard() {
        List<Card> cards = shopMapper.showCard();
        return Result.success(cards);
    }

    /**
     * 商品购买
     * @param card
     * @return
     */
    @Override
    public Result updateStatus(Card card) {
        //修改状态
        Integer integer = shopMapper.updateStatus(card);
        if(integer>0){
            //创建coupon对象
            Coupon coupon = new Coupon();
            //查询
            Coupon mapper = shopMapper.findByCouponId(coupon.getCouponId());
            if(null==mapper){
                return Result.error("查询为空");
            }
            //优惠券使用+1
            mapper.setUseCount(coupon.getUseCount()+1);
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            return Result.success("购买成功"+token);
        }

        return Result.success(integer);
    }

    /**
     * 优惠券的领取
     * @param couponRequest
     * @return
     */
    @Override
    public Result bringCoupon(CouponRequest couponRequest) {
        //获取数量
        Integer useCount = couponRequest.getUseCount();
        //判断优惠券可用数量为0，则提示用户：优惠券已领
        if(useCount==0){
            return Result.error("优惠券领完");
        }
        //修改优惠券的可用数量减一，修改已领取数量加一
        if(useCount>0){
            couponRequest.setUseCount(useCount-1);
            couponRequest.setBringCount(couponRequest.getBringCount()+1);
        }
        return Result.success("领取成功");
    }
}
